<?php
namespace app\api\model;
use phpDocumentor\Reflection\Types\Boolean;
use think\Model;
use think\Db;
use think\Exception;
class Agency extends Model
{
    //上传信息
    public function uploadmessage($data){
        try {
            //开启事务
            Db::startTrans();
            $insert1 = Db::name('agency')->insertGetId([
                'shop' => $data['shop'],
                'name' => $data['name'],
                'iphone' => $data['iphone'],
                'prefix' => $data['shop']
            ]);
            $insert2 = Db::name('businesscertificate')->insertGetId([
                'agencyid' => $insert1
            ]);
            if (!($insert1 !== false && $insert2 !== false)) {
                throw new Exception('出现意外');
            }
            Db::commit();
            return $insert2;
        }
        catch (Exception $e) {
            //如获取到异常信息，对所有表操作，都会回滚至操作前的状态：
            Db::rollback();
            return 0;
        }
    }
    public function upload($id,$photo,$line){
        $info = $photo->move(ROOT_PATH . 'public' . DS . 'static/img_agency');
        $db = Db::name('businesscertificate')
            ->update([
                'id' => $id,
                $line => $info->getSaveName()
            ]);
        if($db){
            return 1;
        }else{
            return 0;
        }

    }
    public function pushwater($id,$data){
        $db = Db::name('pushwater p')
            ->join('indent i','p.indentid=i.id','left')
            ->join('issue e','i.issueid=e.id','left')
            ->where('p.agencyid',$id)
            ->where('e.name','like','%'.$data.'%')
            ->field('e.photo,e.name,count(p.indentid) countindent,sum(i.reality) aggregate,sum(agencypush) push')
            ->group('issueid')
            ->select();
        if($data!=null){
            if($db){
                return $db;
            }else{
                return 0;
            }
        }
        $zong = 0;
        foreach($db as $v){
            $zong+=$v['push'];
        }
        $array = array_merge(['zong'=>$zong],$db);
        return $array;
    }
    public function recommend($id,$data){
        $db = Db::name('user')
            ->where('agencyid',$id)
            ->where('iphone|name|times','like','%'.$data.'%')
            ->field('photo,name,iphone,times')
            ->select();
        if($data!=null){
            if($db){
                return $db;
            }else{
                return 0;
            }
        }
        $db2 = Db::name('user')->where('agencyid',$id)->field('count(agencyid) zong')->find();
        $array = array_merge($db2,$db);
        return $array;
    }
    public function indentwater($id,$data){
        $db = Db::name('indent i')
            ->join('user u','i.userid=u.id','left')
            ->where('i.agencyid',$id)
            ->where('i.dnumber|u.name|i.reality','like','%'.$data.'%')
            ->field('u.photo,u.name,u.iphone,i.reality,i.audit')
            ->select();
        if($data!=null){
            if($db){
                return $db;
            }else{
                return 0;
            }
        }
        $db2 = Db::name('indent')->where('agencyid',$id)->field('count(agencyid) zong')->find();
        $array = array_merge($db2,$db);
        return $array;
    }
    //总收益（当前收益）
    public function earnings($id,$data){
        if (preg_match('/^[\x{4e00}-\x{9fa5}]+$/u',$data)) {
            if($data=='');
        } else {
            $db1 = Db::name('indent')
                ->where('agencyid',$id)
                ->where('dnumber|reality','like','%'.$data.'%')
                ->field('dnumber,uptimes,reality')
                ->select();
            $db2 = Db::name('draw')
                ->where('power',2)
                ->where('userid',$id)
                ->where('money','like','%'.$data.'%')
                ->field('status,create_time,money')
                ->select();
            $array = array_merge($db1,$db2);
        }
        return $array;
    }
    //我的商铺
    public function myshop($id){
        $db1 = Db::name('agency a')
            ->where('a.id',$id)
            ->join('indent i','a.id=i.agencyid','left')
            ->join('user u','a.id=u.isagency','left')
            ->field('shop,a.iphone,a.region,work,money,count(i.agencyid) allindent')
            ->find();
        $db2 = Db::name('pushwater')
            ->where('agencyid',$id)
            ->field('sum(agencypush) allpush')
            ->find();
        $db3 = Db::name('user')
            ->where('agencyid',$id)
            ->field('count(id) allrecommend')
            ->find();
        $db4 = Db::name('pushwater')
            ->where('agencyid',$id)
            ->whereTime('time', 'today')
            ->field('sum(agencypush) todaypush')
            ->find();
        $db5 = Db::name('indent')
            ->where('agencyid',$id)
            ->whereTime('times', 'today')
            ->field('count(id) todyident')
            ->find();
        $array = array_merge($db1,$db2,$db3,$db4,$db5);
        return $array;
    }
}